package traverser;

import java.util.Arrays;
import java.util.HashMap;

public class KeyTracker {
	public static final String[] stopwords = { "of", "the", "for", "and",
			"with", "or", "list" };

	private String endLink;
	private HashMap<String, Integer> keys;
	private CacheLinkProcessor p;
	private String[] targetKeyword;

	public KeyTracker(String url) {
		endLink = url;
		splitTarget();
		fillHashMap();
	}

	public void splitTarget() {
		String tempEndLink = endLink;
		tempEndLink = tempEndLink.substring(29);
		tempEndLink = tempEndLink.replace("of_", "");
		tempEndLink = tempEndLink.replace("the_", "");
		tempEndLink = tempEndLink.replace("and_", "");
		tempEndLink = tempEndLink.replace("with_", "");
		tempEndLink = tempEndLink.replace("or_", "");
		tempEndLink = tempEndLink.replace("or_", "");
		tempEndLink = tempEndLink.replace("(", "");
		tempEndLink = tempEndLink.replace(")", "");
		tempEndLink = tempEndLink.replace("%27", "'");
		tempEndLink = tempEndLink.toLowerCase();
		targetKeyword = tempEndLink.split("_");

		/*
		 * for (String str : targetKeyword) { System.out.println(str); }
		 */

		// TODO: rather than use string.replace() (which uses regex and so will
		// be slow) it's
		// probably better to split it first and check each of the substrings
		// against your
		// stop words.
	}

	public static String[] splitAndReduce(String link) {
		link = link.substring(29);
		link = link.toLowerCase();
		link = link.replace("(", "");
		link = link.replace(")", "");

		String[] results = link.split("_");
		int end = results.length - 1;

		// Remove stopwords (the, and, etc) from the list
		for (int i = 0; i < end; i++) {
			for (String stop : stopwords) {
				if (results[i].equals(stop)) {
					results[i] = results[end];
					end--;
					i--;
					break;
				}
			}
		}

		String[] temp = new String[end + 1];
		for (int i = 0; i < temp.length; i++) {
			temp[i] = results[i];
		}
		results = temp;

		return results;
	}

	public void fillHashMap() {
		String[] tempLink;
		keys = new HashMap<String, Integer>();
		p = new CacheLinkProcessor(this.endLink);
		tempLink = p.getLinks();
		String[][] keyWord = new String[tempLink.length][];
		int size = 0;

		for (int i = 0; i < tempLink.length; i++) {
			/*
			 * String str=tempLink[i]; str=str.substring(29,str.length());
			 * str=str.replace("of_",""); str=str.replace("the_","");
			 * str=str.replace("and_",""); str=str.replace("with_","");
			 * str=str.replace("or_",""); str=str.replace("or_","");
			 * str=str.replace("(",""); str=str.replace(")","");
			 * str=str.replace("%27","'"); str=str.toLowerCase();
			 * keyWord[i]=str.split("_");
			 */
			keyWord[i] = splitAndReduce(tempLink[i]);
			size += keyWord[i].length;

			// TODO: Same as above - split and then replace
		}

		String[] keyword_temp = new String[size];
		int z = 0;

		/*
		 * for(int i=0;i<keyWord.length;i++){ System.out.println(""); for(int
		 * j=0;j<keyWord[i].length;j++){ System.out.print(keyWord[i][j]+"  "); }
		 * }
		 */

		for (int i = 0; i < keyWord.length; i++) {
			for (int j = 0; j < keyWord[i].length; j++) {
				boolean b = false;
				for (int k = 0; k < z; k++) {
					if (keyword_temp[k].equals(keyWord[i][j])) {
						b = true;
					}
				}
				if (b == false) {
					keyword_temp[z] = keyWord[i][j];
					z++;
				}
			}
		}

		String[] keyword = new String[z];

		for (int i = 0; i < z; i++) {
			keyword[i] = keyword_temp[i];
		}

		/*
		 * for(int i=0;i<keyword2.length;i++){
		 * System.out.print(keyword2[i]+" , "); }
		 */

		// TODO: Note that we have variables keyword and keyWord - this is
		// TERRIBLE
		// coding style, for what should be obvious reasons.

		// Note: z == keyword.length
		int[] counter = new int[z];
		for (int i = 0; i < keyword.length; i++) {
			counter[i] = 0;
			for (int j = 0; j < keyWord.length; j++) {
				for (int k = 0; k < keyWord[j].length; k++) {
					if (keyword[i].equals(keyWord[j][k])) {
						counter[i]++;
					}
					/*
					 * for(int l=0;l<targetKeyword.length;l++){ //give high
					 * priority to the target keyword
					 * if(keyword[i].equals(targetKeyword[l])){
					 * counter[i]=10000; } }
					 */

				}
			}
		}
		// TODO: the way I would handle this is just put everything into the
		// hashmap directly,
		// don't even bother with the arrays.

		/*
		 * for(int i=0;i<keyword.length;i++){
		 * System.out.print(keyword[i]+" , "); }
		 */
	
		
		/*
		 * Edit by Dong to solve homonyms. I rewrite the counter, not by the
		 * frequency, reduce the difference between different keywords. And hope
		 * it can help reduce the effect brought by the problem of homonyms. hope it can help. The formal
		 * program is below, we can compare them by data 1/04/2012
		 */
		int []a =new int[10000];
		for(int i=0;i<z;i++){
			a[counter[i]]=1;
		}
		int sum=0;
		HashMap<String, Integer> sequence=new HashMap();
		for(int i=0;i<10000;i++){
			if(a[i]!=0){
				sum++;
				sequence.put(Integer.toString(i),sum);
			}
		}
		for(int i=0;i<keyword.length;i++){
		    keys.put(keyword[i], sequence.get(Integer.toString(counter[i])));
		}
//		
// This is the former program: It has large difference between different keywords.
//		for(int k=0;k<keyword.length;k++){ 
//			  keys.put(keyword[k], counter[k]); 
//			  }
//		
//		for(int k=0;k<keyword.length;k++){
//			System.out.print(keyword[k]);
//			int miku=keys.get(keyword[k]);
//			System.out.print(" "+miku);
//			System.out.println();
//		}
			// System.out.println("keyword: "+keyword[i]+" :priority "+returnHashmap(keyword[i]));
		}

	

	public int priorityOf(String word) {
		Object o = keys.get(word);
		if (o == null)
			return 0;
		else
			return keys.get(word);
	}

	public static void main(String[] args) {

		String url = "http://en.wikipedia.org/wiki/Robot";
		KeyTracker ki = new KeyTracker(url);
		System.out.print(ki.priorityOf("autonomous"));
	}

}
